﻿using System;
using JetBrains.Annotations;
using Missbot.AIModule.OnlineStore;
using Missbot.Common;
using Missbot.Core.Base;
using Missbot.Core.Cache;

namespace Missbot.AIModule.Gaming.SignIn
{
    [UsedImplicitly]
    public class SignInMod : SignInBaseMod
    {
        protected override void Sign(InboxSession session)
        {
            var sign = SignInSuccessiveDA.Sign(session.Group.ID, session.User.ID);
            var goldsGen = Math.Min(sign.SuccessiveDays() * 5, 50);

            OSPersonDA.GoldIncome(session.User.ID, goldsGen);
            var indexNo = SignInGroupInfoDA.GetAndUpdate(session.Group.ID);

            var buffer = new OutputBuffer(session);
            buffer.Add($"签到成功！你已连续签到 {sign.SuccessiveDays()}天，获得 {goldsGen.CurencyFormat()}！");
            buffer.Add($"本群签到排名：【No.{indexNo}】");
            if (sign.SuccessiveDays() % 10 == 0)
            {
                var cache = PersonCacheRecordDA.Get(session.User.ID, "抽奖");
                cache.Value = !string.IsNullOrEmpty(cache.Value) && int.TryParse(cache.Value, out var times) ? (times + 1).ToString() : 1.ToString();
                cache.Update();

                buffer.Add("恭喜你获得一次抽奖机会，快去试试吧（当日有效！）");
            }

            buffer.Send();
        }
    }
}